<?php

namespace Service\TraitService;

use Service\Code\Normal;
use Service\ChannelService\ConfigService;
use OSS\OssClient;

/**
 * 阿里云文件上传服务
 * @author chenshubo
 */
trait AliYunOssFileUploadTrait
{
    private static $mimeTypes = 
    [
        'bmp'   => 'image/bmp',
        'gif'   => 'image/gif',
        'jpeg'  => 'image/jpeg',
        'jpg'   => 'image/jpeg'
    ];
    
    /**
     * 文件文件流
     * @param string $contents
     * @return mixed
     */
    public function uploadByContents($file,$filePath="sbbc/upload/")
    {
        try {
            
            $allowedArr = ['jpeg', 'gif', 'jpg', 'png','xls','xlsx'];
            $ext = strtolower($file->getExtension());
            if (!in_array($ext, $allowedArr)) {
                return Normal::returnCode(Normal::FAIL,"当前上传格式[{$ext}]不支持");
            }
            
            // 配置
            $config = ConfigService::instance()->getConfig((int)env('SID'),'ali_oss');
            $newFile  = $filePath . date("YmdHis"). rand(100000,999999) . '.' . $ext;
            $filePath = $file->getPathname();
            $ossClient = new OssClient($config['oss_key_id'],$config['oss_key_secret'],$config['oss_endpoint']);
            $options = array(
                OssClient::OSS_HEADERS => array(
                    'x-oss-object-acl' => 'public-read',
                ),
            );
            $resp = $ossClient->uploadFile($config['oss_bucket'], $newFile, $filePath, $options);
            if( empty($resp['info']['url']) ) {
                $error = json_encode($resp);
                return Normal::returnCode(Normal::FAIL,"上传文件失败[{$error}]");
            }
            
            return Normal::returnCode(Normal::SUC,"处理完成",[
                'ext'   => $ext,
                'name'  => $filePath,
                'upname'=> $filePath,
                'path'   => $config['url'].'/'.$newFile
            ]);
        } catch ( \Throwable $th ) {
            return Normal::returnCode(Normal::FAIL,"文件处理异常[{$th->getMessage()},{$th->getFile()},{$th->getLine()}]");
        }
    }
    
    /**
     * docker 容器内上传文件流
     * @param string $contents
     * @return mixed
     */
    public function uploadByTempLocalFile($file,$filePath="sbbc/channel/upload/")
    {
        try {
            $pathInfo = pathinfo($file);
            if( $pathInfo['extension'] != 'xlsx' ) {
                return Normal::returnCode(Normal::FAIL,"导出数据格式仅支持xlsx");
            }
            $ext        = $pathInfo['extension'];
            // 配置
            $config     = ConfigService::instance()->getConfig((int)env('SID'),'ali_oss');
            $newFile    = $filePath. $file;
            $filePath   = $file;
            $ossClient  = new OssClient($config['oss_key_id'],$config['oss_key_secret'],$config['oss_endpoint']);
            $options = array(
                OssClient::OSS_HEADERS => array(
                    'x-oss-object-acl' => 'public-read',
                ),
            );
            $resp = $ossClient->uploadFile($config['oss_bucket'], $newFile, $filePath, $options);
            if( empty($resp['info']['url']) ) {
                $error = json_encode($resp);
                return Normal::returnCode(Normal::FAIL,"上传文件失败[{$error}]");
            }
    
            return Normal::returnCode(Normal::SUC,"处理完成",[
                'ext'       => $ext,
                'name'      => $file,
                'upname'    => $file,
                'path'      => $config['url'].'/'.$newFile
            ]);
        } catch ( \Throwable $th ) {
            return Normal::returnCode(Normal::FAIL,"文件处理异常[{$th->getMessage()},{$th->getFile()},{$th->getLine()}]");
        }
    }
}